package edu.gatech.gitmad.catbug.formation;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MapActivity extends Activity{
	private LocationManager locationManager;
	private boolean gpsEnabled;
	private Geocoder geocoder;
	public static boolean soFreshSoClean = false; // fresh coords
	public static double mCurrentLat;
    public static double mCurrentLong;
    public static Location mLocation = null;
    
    private TextView debugger;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);
		
		debugger = (TextView) findViewById(R.id.debug_text_view);
		
		// set up location services
        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        //Get location services
        findLocation();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.create_group, menu);
		return true;
	}
	
	private void findLocation() {
		// alert dialog that requests that the user enable the location services, 
    	// then when the user clicks the "OK" button, calls enableLocationSettings()
		AlertDialog.Builder gpsOk = new AlertDialog.Builder(MapActivity.this);
        gpsOk.setTitle(R.string.gps_ok);
        gpsOk.setCancelable(true);
        gpsOk.setPositiveButton(R.string.prompt_confirm, new DialogInterface.OnClickListener() {
        	public void onClick(DialogInterface dialog, int id) {
        		if (!gpsEnabled) {
        			// enableLocationSettings();
        			gpsEnabled = true;
        		}
        		geocoder = new Geocoder(MapActivity.this);
        		LocationProvider provider = locationManager.getProvider(LocationManager.GPS_PROVIDER);
        		if (provider!= null) {
        			mLocation = null;
        			LocationListener locationListener = new LocationListener() {
        			    @Override
        			    public void onLocationChanged(Location location) {
        			        // A new location update is received
        			    	// Update mLocation
        			    	if (location != null) {
        			    		mLocation = location;
        			    		Log.d("tits", "Got dat location shit");
        			    		mCurrentLat = location.getLatitude();
        			            mCurrentLong = location.getLongitude();
        			            soFreshSoClean = true;
        			            //debugger.setText(mCurrentLat + "," + mCurrentLong);
        			    	}
    			    		else {
    			    			Log.d("tits", "location not found");
    			    			mLocation = null;
    			    		}
        			    }

						@Override
						public void onProviderDisabled(String arg0) {}

						@Override
						public void onProviderEnabled(String provider) {}

						@Override
						public void onStatusChanged(String provider, int status, Bundle extras) {}
        			};
        			
        			locationManager.requestLocationUpdates(
        					// update location for at least 2 minutes while the create item form is active
        	                LocationManager.NETWORK_PROVIDER, 10000, 0, locationListener);
        		}
        	}
        });
        
        AlertDialog gpsDialog = gpsOk.create();
        gpsDialog.show();
    }
	
}
